package com.android.kmlmrg.views;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;

import com.android.kmlmrg.R;
import com.android.kmlmrg.adapter.ContactAdapter;
import com.android.kmlmrg.event.RestGetUserEvent;
import com.android.kmlmrg.event.UserDelEvent;
import com.android.kmlmrg.event.UserFieldChangedEvent;
import com.android.kmlmrg.event.UsersAddEvent;
import com.android.kmlmrg.listener.SearchBarListener;
import com.android.kmlmrg.listener.SearchItemListener;
import com.android.kmlmrg.listener.custom.RestGetUserListener;
import com.android.kmlmrg.listener.custom.UserFieldListener;
import com.android.kmlmrg.listener.custom.UsersListener;
import com.android.kmlmrg.model.User;
import com.android.kmlmrg.model.Users;
import com.android.kmlmrg.services.ServiceController;
import com.android.kmlmrg.services.rest.method.GetUser;
import com.android.kmlmrg.utils.MenuBarHelper;

public class Search extends Activity implements UserFieldListener, UsersListener{

	/**LOG**/
	private static final  String LOG_TAG = Search.class.getName();
	
	private  ListView list;
	
	private ContactAdapter adapter;
	
	private EditText searchBar;

	private List<User> listUser;
	/****/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(LOG_TAG, "START - onCreate");
		super.onCreate(savedInstanceState);
		User.addListener(this);
		Users.addListener(this);
		setContentView(R.layout.search);
		setList((ListView)findViewById(R.id.searchListView));
		listUser = ServiceController.getInstance().getUsers().getList();
		adapter = new ContactAdapter(this,listUser);
		list.setAdapter(adapter);
		SearchItemListener searchListener = new SearchItemListener( this );
		list.setOnItemClickListener(searchListener);
		searchBar = (EditText) findViewById(R.id.searchBar);
		SearchBarListener searchBarListener = new SearchBarListener(this);
		searchBar.addTextChangedListener(searchBarListener);
		Log.i(LOG_TAG, "END - onCreate");
	}
	
	
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}



	@Override
	protected void onResume() {
		Log.i(LOG_TAG, "START - onResume");

		Log.i(LOG_TAG, "START - onResume");
		super.onResume();
	
	}



	@Override
	protected void onPause() {
//		Log.i(LOG_TAG, "START - onPause");
//		list = null ;
//		adapter =null;
//		listUser =null;	
//		searchBar = null;
		Log.i(LOG_TAG, "END - onPause");
		super.onPause();
		
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuBarHelper.manageMenuItemClick(item, this);
	}

	public ListView getList() {
		return list;
	}

	public void setList(ListView list) {
		this.list = list;
	}
	
	public ContactAdapter getAdapter(){
		return adapter;
	}

	public List<User> getListUser() {
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	public EditText getSearchBar() {
		return searchBar;
	}

	public void setSearchBar(EditText searchBar) {
		this.searchBar = searchBar;
	}



	public void userFielChanged(UserFieldChangedEvent event) {
		list.invalidateViews();
		
	}



	public void notifiedAddListener(UsersAddEvent pEvent) {
		list.invalidateViews();
		
	}



	public void notifiedDelUserEvent(UserDelEvent event) {
		list.setAdapter(new ContactAdapter(this,ServiceController.getInstance().getUsers().getList()));
	}

}
